﻿using System;
using System.IO;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class admin_news_modi : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //显示展示图片
            TextBox _textBox = (TextBox)FormView1.FindControl("TextBox8");
            Image ImageUpload = (Image)FormView1.FindControl("Image1");
            if (_textBox.Text.Trim() != "")
            {
                ImageUpload.Visible = true;
                ImageUpload.ImageUrl = _textBox.Text.Trim();
            }
            else
            {
                ImageUpload.Visible = false;
            }

            //RadioButtonList _RBL_readstate = (RadioButtonList)FormView1.FindControl("RadioButtonList2");
            //if (useradminid > 0)
            //{
            //    _RBL_readstate.Items.FindByValue("3").Enabled = true;
            //    //_RBL_readstate.SelectedValue = "3";
            //}
            //else
            //{
            //    //_RBL_readstate.SelectedValue = "2";
            //    _RBL_readstate.Items.FindByValue("3").Enabled = false;
            //}

            help.Visible = false;
            //显示展示图片
            //TextBox _textBox = (TextBox)FormView1.FindControl("TextBox8");
            //Image ImageUpload = (Image)FormView1.FindControl("Image1");
            //ImageUpload.Visible = true;
            //ImageUpload.ImageUrl = _textBox.Text.Trim();

            /*TextBox _TextBox_text = (TextBox)FormView1.FindControl("TextBox13");
        
            if (_TextBox_text.Text == "")
            {
                _TextBox_text.Text = "Admin";
            }
        
            _TextBox_text = (TextBox)FormView1.FindControl("TextBox3");
            _TextBox_text.Text = System.DateTime.Now.ToString();

            _TextBox_text = (TextBox)FormView1.FindControl("TextBox14");
            _TextBox_text.Text = "本站原创";
    */

            // help.Visible = false;
            /*
            if (Session["shang_admin"] == null)
            {
                Response.Redirect("get_your_passport.aspx");
            }
        
            quanxian_system_api quanxian = new quanxian_system_api();
            if (quanxian.check_page("news_add.aspx", Session["pageset"].ToString()))
            {
                Response.Redirect("information.aspx?in=wrong&returnurl=news_manage.aspx");
            }*/

            /*
            if (!IsPostBack)
            {
                if (Request.Cookies["zhaiyao"] != null)
                {
                    TextBox9.Text = Server.UrlDecode(Request.Cookies["zhaiyao"].Value);
                }

                Label1.Text = "";
                TextBox3.Text = DateTime.Now.ToString();
                help.Visible = false;
                TextBox4.Text = Session["shang_admin"].ToString();
                TextBox4.ReadOnly = true;
            }
             * */
            // Label1.Text = FormView1.cont.Row.; //.DataKeyNames.ToString(); //.DataItem.ToString();
        }
    }


    protected void Button1_Click(object sender, EventArgs e)
    { /* 
        if (FormView1.DataItem. FormView1.DataItem.ToString()  == "")
        {
            Label1.Text = "添加新闻出错,可能你的新闻标题未填写";
        }
        else if (80 > 50)
        {
            Label1.Text = "博客文章标题长度过长,请缩短后再填写";
        }
        else
        {
           
            try
            {
                string content = FCKeditor1.Value;
                string oldconten = FCKeditor1.Value;
                content = Regex.Replace(content, "'", "shangduguojing");
                TextBox9.Text.Replace(",", "");

                //使用函数API

                shang_cms_news newsinfor = new shang_cms_news();
                newsinfor.title = Format(TextBox1.Text);
                newsinfor.bigclassname = Convert.ToInt32(DropDownList1.Text);
                try
                {
                    newsinfor.smallclass = Convert.ToInt32(DropDownList3.Text);
                }
                catch
                {
                    newsinfor.smallclass = 0;
                }
                newsinfor.url = TextBox2.Text;
                newsinfor.author = TextBox4.Text;
                newsinfor.time = Convert.ToDateTime(TextBox3.Text);
                newsinfor.content = Format(content);
                newsinfor.color = DropDownList2.Text;
                newsinfor.ding = Convert.ToInt32(DropDownList5.Text);
                newsinfor.zhaiyao = TextBox9.Text;
                newsinfor.tag = TextBox5.Text;
                newsinfor.xiangguan = TextBox6.Text;
                newsinfor.smallclassname = TextBox8.Text;
                newsinfor.usertete = "";
                news_system_api news = new news_system_api();
                if (DropDownList3.Text == "" || DropDownList4.Text == "0")
                {
                    news.insert_news_without_smallclass_returnid(newsinfor);
                }
                else
                {
                    news.insert_news_with_smallclass_returnid(newsinfor);
                }
                string newid = newsinfor.bh.ToString();
                shang_cms_example templaten = new shang_cms_example();
                news.get_news_template(newsinfor, templaten);
                string template = templaten.example;

                if (ConfigurationManager.AppSettings["active"].ToString() == "1")
                {
                    if (DropDownList3.Text == "" || DropDownList4.Text == "0")
                    {
                        StreamWriter sw = File.CreateText(Server.MapPath("../articls/" + DropDownList1.Text + "/" + newid + ".aspx"));
                        sw.Write(template);
                        sw.Close();
                    }
                    else
                    {
                        StreamWriter sw = File.CreateText(Server.MapPath("../articls/" + DropDownList1.Text + "/" + DropDownList3.Text + "/" + newid + ".aspx"));
                        sw.Write(template);
                        sw.Close();
                    }
                }

                HttpCookie cookies_zhaiyao = new HttpCookie("zhaiyao");
                cookies_zhaiyao.Value = Server.UrlEncode(TextBox9.Text);
                cookies_zhaiyao.Expires = DateTime.Now.AddDays(10);
                Response.AppendCookie(cookies_zhaiyao);

                shang_cms_diary diary = new shang_cms_diary();
                diary.name = Session["shang_admin"].ToString();
                diary.actiontime = DateTime.Now;
                diary.action = Session["shang_admin"].ToString() + "成功的添加了名为" + newsinfor.title + "的新闻";
                diary.ip = Page.Request.UserHostAddress.ToString();
                diary_system_api action = new diary_system_api();
                action.diary_insert(diary);

                Response.Redirect("news_manage.aspx?bigclassname=" + DropDownList1.Text + "");
            }
            catch(Exception ee)
            {
                Label1.Text = ee.ToString();
            }* */

    }

    //}


    protected void Button2_Click(object sender, EventArgs e)
    {
        // help.Visible = true;
    }
    protected void Button5_Click1(object sender, EventArgs e)
    {
        //help.Visible = false;
    }



    protected void Button3_Click(object sender, EventArgs e)
    {
        FileUpload fileupload_pic = (FileUpload)FormView1.FindControl("MyFileUpload");
        if (fileupload_pic.PostedFile.FileName.ToString() != "")
        {
            Label label_Msg = (Label)FormView1.FindControl("Label_Msg");  //提示信息标签

            string photoname = fileupload_pic.PostedFile.FileName;
            int i = photoname.LastIndexOf(".");
            string file_next_name = photoname.Substring(i);
            if (file_next_name.ToLower() != ".gif" && file_next_name.ToLower() != ".jpg" && file_next_name.ToLower() != ".jepg" && file_next_name.ToLower() != ".bmp" && file_next_name.ToLower() != ".png")
            {
                Response.Write("<script language='Javascript'>alert('上传文件出错,并不是图片文件！');</script>");
            }
            else
            {
                string filename = DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Year.ToString() + DateTime.Now.Second.ToString() + file_next_name; // 文件名称
                string filename_s = "s_" + filename; // 缩略图文件名称
                string webFilePath = Server.MapPath("/upload/news/" + filename); // 服务器端文件路径
                string webFilePath_s = Server.MapPath("/upload/news/" + filename_s);　　// 服务器端 中 缩略图路径
                string webFilePath_s_s = Server.MapPath("/upload/news/s_" + filename_s);　　// 服务器端 小 缩略图路径


                if (!File.Exists(webFilePath))
                {
                    try
                    {
                        fileupload_pic.PostedFile.SaveAs(webFilePath); // 使用 SaveAs 方法保存文件
                        //Discuz.Web.global.global.MakeThumbnail(webFilePath, webFilePath_s, 280, 220, "Cut"); // 生成缩略图方法
                        //Discuz.Web.global.global.MakeThumbnail(webFilePath, webFilePath_s_s, 130, 95, "Cut"); // 生成缩略图方法

                        TextBox _TextBox_text1 = (TextBox)FormView1.FindControl("TextBox8");
                        _TextBox_text1.Text = "/upload/news/" + filename_s.Trim();

                        Image ImageUpload = (Image)FormView1.FindControl("Image1");
                        ImageUpload.Visible = true;
                        ImageUpload.ImageUrl = "/upload/news/" + filename_s;


                        label_Msg.Text = "提示：图片上传成功，并生成缩略图，大小：" + fileupload_pic.PostedFile.ContentLength / 1024 + "K";//，文件类型为：" + fileupload_pic.PostedFile.ContentType + "
                    }
                    catch (Exception ex)
                    {
                        label_Msg.Text = "提示：文件上传失败，失败原因：" + ex.Message;
                    }
                }
                else
                {
                    label_Msg.Text = "提示：图片已经存在，请重新上传";
                }
            }
        }
        else
        {
            Response.Write("<script language='Javascript'>alert('上传图片文件不能为空！');</script>");
        }
    }

    /*
        public string Format(object input)
        {
            string data = input.ToString();
            data = data.Replace("'", "&qapos;");
            return data;
        }*/


    protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
    {
        //e.NewValues["userid"] = userid;

        //e.NewValues["subclassid"] = Convert.ToInt32(Request.QueryString["subclassid"].ToString());
        //e.NewValues["mainclassid"] = Convert.ToInt32(Request.QueryString["classid"].ToString());
        //TextBox _TextBox_text = (TextBox)FormView1.FindControl("TextBox13");
        //if (_TextBox_text.Text.Trim() == "")
        //{
        //    e.NewValues["username"] = username.Trim();
        //    //_TextBox_text.Text = "Admin";
        //}

        //DropDownList _DropDownList_Category = (DropDownList)FormView1.FindControl("DropDownList3");
        //if (_DropDownList_Category != null)
        //    e.NewValues["subclassid"] = _DropDownList_Category.SelectedValue;
        //else
        //{
        //    //Response.Write("<script language='Javascript'>alert('分类无效，请选择有效的分类！');</script>");
        //    e.Cancel = true;
        //}

        /*
        TextBox _TextBoxTitle = (TextBox)FormView1.FindControl("TextBox1");
        if (_TextBoxTitle.Text == "")
        {
            Response.Write("<script language='Javascript'>alert('添加新闻出错,可能你的新闻标题未填写！');</script>");
            e.Cancel = true;
        }
        //Response.Write("<script language='Javascript'>alert('添加新闻出错,可能你的新闻标题未填写！');</script>");

        //Label1.Text = e.Values["title"].ToString();
        //string title11 = e.Values["title"].ToString();
        if (e.Values["title"].ToString() == null)
        {
            Response.Write("<script language='Javascript'>alert('添加新闻出错,可能你的新闻标题未填写！');</script>");
            return;
            //Label1.Text = "添加新闻出错,可能你的新闻标题未填写";
        }
         * 
         * 
         * */
        //else if (e.Values["title"] TextBox_Title.Text.Length > 80)
        //{
        //    Response.Write("<script language='Javascript'>alert('文章标题长度过长,请缩短后再填写！');</script>");
        //}


        //if (e.Values[""] FormView1.DataItem.FormView1.DataItem.ToString() == "")
        // {
        //     Label1.Text = "添加新闻出错,可能你的新闻标题未填写";
        // }
        // else if (80 > 50)
        // {
        //     Label1.Text = "博客文章标题长度过长,请缩短后再填写";
        // }
        //DataRowView rowView = (DataRowView)FormView1.DataItem;
        //Label1.Text = FormView1.DataItemCount.ToString();// rowView.Row.ToString();/;/ ["title"].ToString(); //"Good Morning!";

    }
    protected void InsertButton_Click(object sender, EventArgs e)
    {
        //Label1.Text=e.
    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        //Label1.Text = "I have changed!";
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        TextBox _TextBox_text = (TextBox)FormView1.FindControl("TextBox13");
        _TextBox_text.Text = "未知";
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        TextBox _TextBox_text = (TextBox)FormView1.FindControl("TextBox13");
        _TextBox_text.Text = "佚名";
    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        TextBox _TextBox_text = (TextBox)FormView1.FindControl("TextBox13");
        _TextBox_text.Text = "Admin";
    }

    protected void FormView1_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
    {
        //TextBox _TextBoxTitle = (TextBox)FormView1.FindControl("TextBox1");
        //Response.Write("<script language='Javascript'>alert('成功的添加了名为  \"" + e.Values["title"].ToString() + "\"  的新闻！');</script>");
        string url = "news_manage.aspx?subclassid=" + Request.QueryString["subclassid"].ToString().Trim() + "&id=" + Request.QueryString["newsid"].ToString().Trim();
        Response.Redirect(url);


    }
    protected void LinkButton5_Click(object sender, EventArgs e)
    {
        TextBox _TextBox_text = (TextBox)FormView1.FindControl("TextBox14");
        _TextBox_text.Text = "本站原创";
    }
}
